package cn.uexpo.lhz.controller.admin;

import cn.uexpo.lhz.biz.EventActivityBiz;
import cn.uexpo.lhz.biz.EventBiz;
import cn.uexpo.lhz.biz.ItemRulesBiz;
import cn.uexpo.lhz.common.MqttGateway;
import cn.uexpo.lhz.common.oldUtils.PageRequest;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.*;
import cn.uexpo.lhz.hxxbase.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 *规则核验内容
 * @author
 * @since
 */
@Controller
@RequestMapping("/admin/itemRules")
public class ItemRulesController extends BaseController {
    @Autowired
    private ItemRulesBiz itemRulesBiz;

    @Autowired
    private EventBiz eventBiz;

    @Autowired
    private EventActivityBiz eventActivityBiz;

    @Autowired
    private MqttGateway mqttGateway;

//    /*
//    *
//    * 页面列表显示     test
//    */
//    @PostMapping(value="/findPage")
//    @ResponseBody
//    public Result<Object>  findPage(@RequestBody ItemRules itemRules) {
//        List<JSONObject> list = new ArrayList<JSONObject>();
//        JSONObject object = new JSONObject();
//        PageRequest pageRequest = new PageRequest();
//        PageResult result = null;
//        if(!"".equals(itemRules.getPageNum())&&!"".equals(itemRules.getPageSize())) {
//            pageRequest.setPageNum(itemRules.getPageNum());
//            pageRequest.setPageSize(itemRules.getPageSize());
//        }
//
//        //有项目展会名显示该展会规则内容
//        if(!"".equals(itemRules.getItem())&&null!=itemRules.getItem()) {
//            ItemRulesExample itemRulesExample = new ItemRulesExample();
//            ItemRulesExample.Criteria itemRulesCriteria = itemRulesExample.createCriteria();
//            itemRulesCriteria.andItemEqualTo(itemRules.getItem());
//            result=itemRulesBiz.findPage(pageRequest,itemRulesExample);
//
//            EventExample eventExample = new EventExample();
//            EventExample.Criteria eventCriteria = eventExample.createCriteria();
//            eventCriteria.andNameEqualTo(itemRules.getItem());
//            List<Event> events = eventBiz.selectByExample(eventExample);
//            if(null!=events&&events.size()>0){
//                int id = events.get(0).getId();
//                EventActivityExample eventActivityExample = new EventActivityExample();
//                EventActivityExample.Criteria eventExampleCriteria = eventActivityExample.createCriteria();
//                eventExampleCriteria.andEventIdEqualTo(id);
//                List<EventActivity> eventActivities = eventActivityBiz.selectByExample(eventActivityExample);
//
//                if(null!=eventActivities&&eventActivities.size()>0){
//                    for(EventActivity activities:eventActivities){
//
//                        String activitieNmae = activities.getActivityName();
//                        object.put("activitieNmae",activitieNmae);
//                        list.add(object);
//                    }
//
//                }
//            }
//
//        }
//        Result<Object> result1 = new Result<>();
//        result1.setData(list);
//        result1.setData(result);
//      return   this.success(list);
//    }

    /*
     *
     * 页面列表显示     test
     */
//    @PostMapping(value="/findPage")
//    @ResponseBody
//    public HttpResult  findPage(@RequestBody ItemRules itemRules) {
//        List<JSONObject> list = new ArrayList<JSONObject>();
//        JSONObject object = new JSONObject();
//        PageRequest pageRequest = new PageRequest();
//        PageResult result = null;
//        if(!"".equals(itemRules.getPageNum())&&!"".equals(itemRules.getPageSize())) {
//            pageRequest.setPageNum(itemRules.getPageNum());
//            pageRequest.setPageSize(itemRules.getPageSize());
//        }
//
//        //有项目展会名显示该展会规则内容
//        if(!"".equals(itemRules.getItem())&&null!=itemRules.getItem()) {
//            ItemRulesExample itemRulesExample = new ItemRulesExample();
//            itemRulesExample.setOrderByClause("id desc");
//            ItemRulesExample.Criteria itemRulesCriteria = itemRulesExample.createCriteria();
//            itemRulesCriteria.andItemEqualTo(itemRules.getItem());
//            itemRulesCriteria.andStateEqualTo("0");
//            result=itemRulesBiz.findPage(pageRequest,itemRulesExample);
//
//        }
//        return HttpResult.ok(result);
//    }

    /*
     *
     * 活动名称
     */
    @PostMapping(value="/activity")
    @ResponseBody
    public HttpResult  activity (@RequestBody ItemRules itemRules) {
        List<JSONObject> list = new ArrayList<JSONObject>();
        JSONObject object = new JSONObject();

        //有项目展会名显示该展会规则内容
        if(!"".equals(itemRules.getItem())&&null!=itemRules.getItem()) {

            EventExample eventExample = new EventExample();
            EventExample.Criteria eventCriteria = eventExample.createCriteria();
            eventCriteria.andNameEqualTo(itemRules.getItem());
            List<Event> events = eventBiz.selectByExample(eventExample);
            if(null!=events&&events.size()>0){
                int id = events.get(0).getId();
                EventActivityExample eventActivityExample = new EventActivityExample();
                EventActivityExample.Criteria eventExampleCriteria = eventActivityExample.createCriteria();
                eventExampleCriteria.andEventIdEqualTo(id);
                List<EventActivity> eventActivities = eventActivityBiz.selectByExample(eventActivityExample);

                if(null!=eventActivities&&eventActivities.size()>0){
                    for(EventActivity activities:eventActivities){

                        String activitieNmae = activities.getActivityName();
                        object.put("activitieNmae",activitieNmae);
                        list.add(object);
                    }

                }
            }

        }
        return HttpResult.ok(list);
    }



    /**
     * 规则新建
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addRules", method = { RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> addRules(@RequestBody ItemRules itemRules) throws Exception {
        String code = "500";
        String msg = "失败";
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();

        String item = itemRules.getItem();// 项目名称
        String checkContnetName = itemRules.getCheckcontnetname();//核验内容名称
        String activity = itemRules.getActivity();//活动名称
        String label = itemRules.getLabel();//标签
        String startDate = itemRules.getStartdate();//开始日期
        String endDate = itemRules.getEnddate();//结束日期
        String startTime = itemRules.getStarttime();//开始时间
        String endTime = itemRules.getEndtime();//结束时间
        String validity = "0";//默认有效
        String state = "0";//默认正常
        Date createTime = new Date();


        int no = 0;
        //设置状态默认为0 正常
        ItemRules rules = new ItemRules();
        rules.setItem(item);
        rules.setCheckcontnetname(checkContnetName);
        rules.setActivity(activity);
        rules.setLabel(label);
        rules.setStartdate(startDate);
        rules.setEnddate(endDate);
        rules.setStarttime(startTime);
        rules.setEndtime(endTime);
        rules.setValidity(validity);
        rules.setCreatetime(createTime);
        rules.setState(state);
        no = itemRulesBiz.insert(rules);
        if (1 == no) {
            code = "200";
            msg = "成功";
        }
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * mq下发
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/itemRules", method = { RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> itemRules(@RequestBody ItemRules itemRules) throws Exception {
        String code = "500";
        String msg = "失败";
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date = new Date();

        String item = itemRules.getItem();// 项目名称
        List<ItemRules> itemRulesList = null ;
        //有项目展会名查找该展会规则内容
        if(!"".equals(itemRules.getItem())&&null!=itemRules.getItem()) {
            ItemRulesExample itemRulesExample = new ItemRulesExample();
            ItemRulesExample.Criteria itemRulesCriteria = itemRulesExample.createCriteria();
            itemRulesCriteria.andItemEqualTo(itemRules.getItem());
            itemRulesCriteria.andStateEqualTo("0");//状态正常数据
            itemRulesList = itemRulesBiz.selectByExample(itemRulesExample);
//                if(null!=itemRulesList&&itemRulesList.size()>0){
//                    for(ItemRules rules :itemRulesList){
//
//                    }
//                }

        }

        JSONObject jObject = new JSONObject();
        jObject.put("itemRulesList", itemRulesList);
        String data=jObject.toString();
        mqttGateway.sendToMqtt(data,"itemRules");//发送itemRules主题
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }


    /**
     * 修改
     * @param
     * @return
     */
    @PostMapping(value="/edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody ItemRules itemRules) {
        Map<String, Object> map = new HashMap<String, Object>();
        int no = 0;
        String code = "500";
        String msg = "失败";
        int id = itemRules.getId();
        String item = itemRules.getItem();// 项目名称
        String checkContnetName = itemRules.getCheckcontnetname();//核验内容名称
        String activity = itemRules.getActivity();//活动名称
        String label = itemRules.getLabel();//标签
        String startDate = itemRules.getStartdate();//开始日期
        String endDate = itemRules.getEnddate();//结束日期
        String startTime = itemRules.getStarttime();//开始时间
        String endTime = itemRules.getEndtime();//结束时间



        ItemRulesExample itemRulesExample = new ItemRulesExample();
        ItemRulesExample.Criteria itemRulesCriteria = itemRulesExample.createCriteria();
        itemRulesCriteria.andIdEqualTo(id);
        itemRulesCriteria.andStateEqualTo("0");//状态正常数据
        List<ItemRules> itemRulesList = itemRulesBiz.selectByExample(itemRulesExample);

        Date createTime = new Date();
        if(null!=itemRulesList&&itemRulesList.size()>0) {
            itemRules.setCreatetime(createTime);
            no = itemRulesBiz.updateByPrimaryKeySelective(itemRules);
        }else {
            msg = "规则不存在";
        }
        if(1==no) {

            code = "200";
            msg = "成功";

        }
        map.put("code", code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除
     * @param
     * @return
     */
    @PostMapping(value="/del")
    @ResponseBody
    public Map<String, Object> del(@RequestBody ItemRules itemRules) {
        Map<String, Object> map = new HashMap<String, Object>();
        int no = 0;
        String code = "500";
        String msg = "失败";
        int id = itemRules.getId();

        ItemRulesExample itemRulesExample = new ItemRulesExample();
        ItemRulesExample.Criteria itemRulesCriteria = itemRulesExample.createCriteria();
        itemRulesCriteria.andIdEqualTo(id);
        itemRulesCriteria.andStateEqualTo("0");//状态正常数据
        List<ItemRules> itemRulesList = itemRulesBiz.selectByExample(itemRulesExample);

        Date createTime = new Date();
        if(null!=itemRulesList&&itemRulesList.size()>0) {
            itemRules.setCreatetime(createTime);
            itemRules.setState("1");
            no = itemRulesBiz.updateByPrimaryKeySelective(itemRules);
        }else {
            msg = "规则不存在";
        }
        if(1==no) {
            code = "200";
            msg = "成功";

        }
        map.put("code", code);
        map.put("msg",msg);
        return map;
    }

}
